package com.jackstraw.jack_base.util.entity.response;


import com.jackstraw.jack_base.util.entity.BaseEntity;

/**
 * 统一返回对象
 */
public class ResultBody<T> extends BaseEntity<T> {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private T result;

    /**
     * create a new {@code ResultBody}
     */
    public ResultBody() {
    }

    /**
     * create a new {@code ResultBody}
     *
     * @param resultInterface
     */
    public ResultBody(ResultInterface resultInterface) {
        this.code = resultInterface.getCode();
        this.message = resultInterface.getMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    /**
     * 成功
     *
     * @return
     */
    public static ResultBody success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> ResultBody<T> success(T data) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResultInterfaceEnum.OK.getCode());
        rb.setMessage(ResultInterfaceEnum.OK.getMsg());
        rb.setResult(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(ResultInterface resultInterface) {
        ResultBody rb = new ResultBody();
        rb.setCode(resultInterface.getCode());
        rb.setMessage(resultInterface.getMsg());
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResultInterfaceEnum.INTERNAL_SERVER_ERROR.getCode());
        rb.setMessage(message);
        return rb;
    }

    @Override
    public String toString() {
        return "JSONObject.toJSONString(this)";
    }
}
